package com.ming.sportdemo.controller;

import com.alibaba.fastjson.JSON;
import com.ming.sportdemo.bean.Field;
import com.ming.sportdemo.bean.QueryInfo;
import com.ming.sportdemo.config.ServerConfig;
import com.ming.sportdemo.service.FieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

@CrossOrigin
@RestController
public class FieldController {

    @Autowired
    private FieldService fieldService;
    @Autowired
    private ServerConfig serverConfig;

    @RequestMapping("/allField")
    public String getFieldList(HttpServletRequest request,QueryInfo queryInfo){
        System.out.println(queryInfo.getPageNum());
        //获取最大列表数和当前编号
        System.out.println(""+queryInfo.getType());

        int numbers = fieldService.getFieldCounts("%"+queryInfo.getQuery()+"%");
        int pageStart = (queryInfo.getPageNum() - 1) * queryInfo.getPageSize();

        List<Field> fields = fieldService.getAllField("%" + queryInfo.getQuery() + "%","%" + queryInfo.getType() + "%", pageStart, queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",fields);
        String res_string = JSON.toJSONString(res);
        return res_string;
    }

    @RequestMapping("/allFieldName")
    public String allFieldName(){
        List<Field> fields = fieldService.list();
        HashMap<String,Object> map = new HashMap<>();
        List<String> names = new LinkedList<>();
        for (int i = 0; i < fields.size(); i++) {
            names.add(fields.get(i).getFieldName());
        }
        map.put("data",names);
        String res = JSON.toJSONString(map);
        return res;
    }

    @RequestMapping("/addField")
    public String addField(@RequestBody Field field){

        String url = UploadController.baseURL;
        field.setFieldView(url);
        int i = fieldService.addField(field);
        return i>0?"success":"error";
    }

    @RequestMapping("/deleteField")
    public String deleteField(int fieldId){
        int i = fieldService.deleteField(fieldId);
        return i>0?"success":"error";
    }

    @RequestMapping("/getUpdate")
    public String getUpdateField(int fieldId){
        Field field = fieldService.getUpdateField(fieldId);
        String string = JSON.toJSONString(field);
        return string;
    }

    @RequestMapping("/updateField")
    public String updateField(@RequestBody Field field){
        System.out.println(field);
        String url = UploadController.baseURL;
        String baseurl = serverConfig.getPort();
        if((baseurl+url).equals(field.getFieldView())){
            field.setFieldView(url);
        }
        field.setFieldView(field.getFieldView());
        int i = fieldService.updateField(field);
        System.out.println();
        System.out.println(field);
        return i>0?"success":"error";
    }
}
